package com.itheima.health.h5.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.redis.RedisUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @GetMapping("setmeal/findAllSetmeal")
    public Result findAll() {
        List<Setmeal> list = RedisUtil.get("setmeal");
        if (list != null) {
            return new Result(list);
        } else {
            list = setmealService.findAllSetmeal();
            RedisUtil.set("setmeal", list);
            return new Result(list);
        }
    }


    @GetMapping("setmeal/findSetMealDetailById/{id}")
    public Result findSetMealDetailById(@PathVariable("id") int id) {
        return new Result(setmealService.findSetMealDetailById(id));
    }

    @GetMapping("setmeal/findById")
    public Result findById(@RequestParam("id") String id) {
        Setmeal setmeal = setmealService.findById(Integer.parseInt(id));
        return new Result(setmeal);
    }
}
